package homewoek12月12号.homewoek;

import cn.itcast.xml.XmlUtils;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Test {
   static List<Student> stlist = XmlUtils.readAll("D:\\ldea dashuju\\day.12.12\\src\\czxy\\homewoek\\student.xml", Student.class);
   static List<Class> clsit = XmlUtils.readAll("D:\\ldea dashuju\\day.12.12\\src\\czxy\\homewoek\\classes.xml", Class.class);
    public static void main(String[] args) throws Exception {
        for (Class s : clsit) {
            for (Student c : stlist) {
                if (s.getClassID().equals(c.getClassID())){
                    s.getSlist().add(c);
                }
            }
        }
        run1();
        run2();
        run3();
        run4();
        run5();
        run6();
    }
    public static void run6() throws IOException {
        for (Class s : clsit) {
            int a=0;
            for (Student s1 : s.getSlist()) {
                a++;
                if (a==1){
                    FileUtils.writeStringToFile(new File(s.getClassname()+".txt"),s1.getClassID()+","+s1.getName()+","+s1.getGender()+","+s1.getNumber()+","+s1.getAge()+","+s1.getMath()+"\n",false);
                }else {
                    FileUtils.writeStringToFile(new File(s.getClassname()+".txt"),s1.getClassID()+","+s1.getName()+","+s1.getGender()+","+s1.getNumber()+","+s1.getAge()+","+s1.getMath()+"\n",true);
                }
            }
        }
    }
    public static void run5(){
        for (Class a : clsit) {
            a.getSlist().sort(new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    if (o1.getAge()-o2.getAge()==0){
                      return o1.getMath()-o1.getMath();
                    }else {
                        return o1.getAge()-o2.getAge();
                    }
                };
            });
            System.out.println(a.getSlist());
        }
    }
    public static void run4(){
        ArrayList<Integer> list = new ArrayList<>();
        for (Class s : clsit) {
            int ge=0;
            for (Student s1 : s.getSlist()) {
                if (s1.getAge()<18){
                    ge++;
                }
            }
            list.add(ge);
        }
        System.out.println(list);
        for (int i = 0; i < clsit.size(); i++) {
                System.out.println(clsit.get(i).getClassname()+"  "+list.get(i));
        }
    }
    public static void run3(){
        ArrayList<Integer> list = new ArrayList<>();
        for (Class s : clsit) {
            int ge=0;
            for (Student s1 : s.getSlist()) {
                if (s1.getMath()<60){
                    ge++;
                }
            }
            list.add(ge);
        }
        System.out.println(list);
        int min=list.get(0);
        int a=0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i)<min){
                min=list.get(i);
                a=i;
                break;
            }
        }
        for (int i = 0; i < clsit.size(); i++) {
            if (i==a){
                System.out.println(clsit.get(i).getClassID()+"  "+clsit.get(i).getClassname()+"  "+clsit.get(i).getAssistant()+"   "+clsit.get(i).getInstructor()+"   "+clsit.get(i).getTeacher());
            }
        }
    }
    public static void run2(){
        ArrayList<Double> list = new ArrayList<>();
        for (Class s : clsit) {
            int sum=0;
            int ge=0;
            double a=0;
            for (Student s1 : s.getSlist()) {
                sum+=s1.getMath();
                ge++;
            }
            a=sum/ge;
            list.add(a);
        }
        System.out.println(list);
        double max=list.get(0);
        int b=0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i)<max){
                b=i;
                max=list.get(i);
            }
        }
        for (int i = 0; i < clsit.size(); i++) {
            if (i==b){
                System.out.println(clsit.get(i).getClassID()+"  "+clsit.get(i).getClassname()+"  "+clsit.get(i).getAssistant()+"   "+clsit.get(i).getInstructor()+"   "+clsit.get(i).getTeacher());
            }
        }
    }
    public static void run1(){
        ArrayList<Double> list = new ArrayList<>();
        for (Class s : clsit) {
            int sum=0;
            int ge=0;
            double a=0;
            for (Student s1 : s.getSlist()) {
                sum+=s1.getAge();
                ge++;
            }
            a=sum/ge;
            list.add(a);
        }
        System.out.println(list);
        double max=list.get(0);
        int b=0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i)>max){
                b=i;
                max=list.get(i);
            }
        }
        for (int i = 0; i < clsit.size(); i++) {
            if (i==b){
                System.out.println(clsit.get(i).getClassID()+"  "+clsit.get(i).getClassname()+"  "+clsit.get(i).getAssistant()+"   "+clsit.get(i).getInstructor()+"   "+clsit.get(i).getTeacher());
            }
        }
    }
}
